package com.mkvld.appietijdenv2;

import com.mkvld.appietijdenv2.activities.ActMainV9;
import com.mkvld.appietijdenv2.adapters.AdapPeriodFrags;
import com.mkvld.appietijdenv2.data.Preferences;
import com.mkvld.appietijdenv2.fragments.FragExtrasKPTV9;
import com.mkvld.appietijdenv2.fragments.FragExtrasV9;
import com.mkvld.appietijdenv2.fragments.FragPayrollV9;
import com.mkvld.appietijdenv2.fragments.FragPayrollKPTV9;
import com.mkvld.appietijdenv2.fragments.PeriodFragV9;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;

public class ActMainNormal extends ActMainV9 {
	ViewPager mViewPager;
	AdapPeriodFrags adapter;
	PeriodFragV9[] frags;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pager);
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		FragShiftsV14 fragShifts = new FragShiftsV14();
		
		FragPayrollV9 fragPayroll = null;
		FragExtrasV9 fragExtras = null;
		if (Preferences.getContract(this) <= 48) {
			fragPayroll = new FragPayrollKPTV9();
			fragExtras = new FragExtrasKPTV9();
		} else {
			fragPayroll = new FragPayrollV9();
			fragExtras = new FragExtrasV9();
		}
		
		frags = new PeriodFragV9[] {
			fragShifts, fragPayroll, fragExtras
		};
		
		int[] fragTitles = new int[] {
			R.string.frag_name_shifts,
			R.string.frag_name_payroll,
			R.string.frag_name_extras
		};
		
		mViewPager = (ViewPager)findViewById(R.id.pager);
		adapter = new AdapPeriodFrags(getSupportFragmentManager(), frags, null);
		mViewPager.setAdapter(adapter);
		mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				getActionBar().setSelectedNavigationItem(position);
			}
			@Override public void onPageScrolled(int arg0, float arg1, int arg2) {}
			
			@Override public void onPageScrollStateChanged(int arg0) {}
		});
		
		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
		ActionBar.TabListener tabListener = new ActionBar.TabListener() {
			@Override public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
			
			@Override
			public void onTabSelected(Tab tab, FragmentTransaction ft) {
				mViewPager.setCurrentItem(tab.getPosition());
			}
			
			@Override public void onTabReselected(Tab tab, FragmentTransaction ft) {}
		};
		
		for (int i = 0; i < frags.length; i++) {
			actionBar.addTab(actionBar.newTab()
					.setText(fragTitles[i])
					.setTabListener(tabListener));
		}
		mViewPager.setCurrentItem(getIntent().getIntExtra("tab", 0));
	}

	@Override
	public void update() {
		super.update();
		for (PeriodFragV9 frag : frags) {
			frag.update();
		}
	}	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected boolean shouldInit() {
		return false;
	}

}
